package com.peas.platform.module.icims.base.mdm.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.peas.platform.common.core.dto.ResponseDto;
import com.peas.platform.common.core.utils.MultipartFileUtil;
import com.peas.platform.module.icims.base.mdm.api.req.BatchDeleteReq;
import com.peas.platform.module.icims.base.mdm.api.req.BatchMoveReq;
import com.peas.platform.module.icims.base.mdm.controller.criteria.FileCriteria;
import com.peas.platform.module.icims.base.mdm.dto.FileDto;
import com.peas.platform.module.icims.base.mdm.service.FileService;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipOutputStream;

@Api(tags = "自定义接口-附件相关接口(包含上传下载)")
@RestController
@RequestMapping("/api/v1/customFile")
@Slf4j
public class CustomFileController {

    @Resource
    private HttpServletRequest request;
    @Resource
    private HttpServletResponse response;

    @Resource
    private FileService fileService;

    @ApiOperation(value = "附件查询列表")
    @GetMapping(value = "/findFileList")
    public ResponseDto<List<FileDto>> findFileList(FileCriteria condition,
                                                   @ApiParam(name = "sortField", value = "排序字段", example = "id") String sortField,
                                                   @ApiParam(name = "sortType", value = "排序方式", example = "desc") String sortType) {
        sortField = StrUtil.isBlank(sortField) ? "id" : sortField;
        OrderItem order = StrUtil.equalsIgnoreCase("asc", sortType) ? OrderItem.asc(sortField) : OrderItem.desc(sortField);
        FileDto c = BeanUtil.copyProperties(condition, FileDto.class);
        List<FileDto> list = fileService.findFileListByDir(order, c);
        return ResponseDto.getSuccessResponseDto(list);
    }

    @ApiOperation(value = "批量移动文件")
    @PostMapping(value = "/moves")
    public ResponseDto<Boolean> moves(@RequestBody @Valid BatchMoveReq vo) {
        List<Long> list = new ArrayList<>();
        for (String id : vo.getIds().split(StrUtil.COMMA)) {
            list.add(Long.valueOf(id));
        }
        boolean result = fileService.moves(list, vo.getParentId());
        return ResponseDto.getSuccessResponseDto(result);
    }

    @ApiOperation(value = "批量删除文件")
    @PostMapping(value = "/deletes")
    public ResponseDto<Boolean> deletes(@RequestBody @Valid BatchDeleteReq vo) {
        List<Long> list = new ArrayList<>();
        for (String id : vo.getIds().split(StrUtil.COMMA)) {
            list.add(Long.valueOf(id));
        }
        boolean result = fileService.removeBatchByIds(list);
        return ResponseDto.getSuccessResponseDto(result);
    }

    @SneakyThrows
    @ApiOperation(value = "批量下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个附件ID(半角逗号分隔)"),
    })
    @GetMapping(value = "/downloads")
    public void downloads(@RequestParam("ids") String ids) {
        List<Long> list = new ArrayList<>();
        for (String id : ids.split(",")) {
            list.add(Long.valueOf(id));
        }
        MultipartFileUtil.initDownloadHeader(request, response, "文件打包下载-" + DateUtil.today() + ".zip");
        ZipOutputStream zip = new ZipOutputStream(response.getOutputStream());
        List<FileDto> attachments = fileService.listByIds(list);
        fileService.zipFiles(attachments, zip, "");
    }

}
